package beans;

import java.util.ArrayList;

public class CartBean {
	private ArrayList<CartItemBean> items = null;
	
	public CartBean(){
		items = new ArrayList<CartItemBean>();
	}

	public ArrayList<CartItemBean> getItems() {
		return items;
	}

	public void setItems(ArrayList<CartItemBean> items) {
		this.items = items;
	}
	
	public void add(CartItemBean item){
		//if the product has been added into the cart before, just change the number
		for(int i = 0; i < items.size(); i++){
			if(items.get(i).getProduct().getID() != item.getProduct().getID())
				continue;
			items.get(i).setNumber(items.get(i).getNumber() + item.getNumber());
			return;
		}
		items.add(item);
	}
	
	//change the number of one type of product in the cart or remove it.
	public void remove(int productID, int number){	
		for(int i = 0; i < items.size(); i++){
			if(items.get(i).getProduct().getID() == productID){		
				if(number <= 0){
					items.remove(i);
					return;
				}else{
					items.get(i).setNumber(number);
					return;
				}
			}
		}
	}
}
